﻿/// <reference path="myzoom.d.ts" />
import ko = require("knockout");

function distance(pointerpoint1, pointerpoint2) {
    var x1 = pointerpoint1.position.x;
    var x2 = pointerpoint2.position.x;
    var y1 = pointerpoint1.position.y;
    var y2 = pointerpoint2.position.y;

    return Math.sqrt(
        (x1 - x2) * (x1 - x2) +
        (y1 - y2) * (y1 - y2)
        );
}

class MyZoom {
    static create() {
        ko.bindingHandlers.myZoom = {
            init: (element, valueAccessor, allBindingsAccessor, viewModel) => {
                element.zoomPointers = {};
                /*document.addEventListener("pointerdown", (event) => {
                    if (Object.keys(element.zoomPointers).length >= 2) return;
                    else {
                        element.zoomPointers[event.pointerId] = event.getCurrentPoint(element);
                    }

                    if (element.zoomPointers.length == 2) {
                        for (var key in element.zoomPointers) {
                            element.setPointerCapture(element.zoomPointers[key]);
                            break;
                        }
                        element.zoomPointers[event.pointerId] = event.getCurrentPoint(element);
                        element.setPointerCapture(event.pointerId);
                    }
                }, true);*/
                document.addEventListener("MSContentZoom", (event) => {
                    valueAccessor().call(element, viewModel, event);
                }, true);
                /*element.addEventListener("MSAnimationStart", (event) => {
                    valueAccessor()(viewModel, event);
                }, true);
                /*document.addEventListener("pointermove", (event) => {
                    if (!(event.pointerId in element.zoomPointers)) return;
                    var points = element.zoomPointers;
                    var pIds = element.zoomPointers.keys;

                    var d1 = distance(points[pIds[0]], points[pIds[1]]);

                    element.zoomPointers[event.pointerId] = event.getCurrentPoint(element);
                    points = element.zoomPointers;

                    var d2 = distance(points[pIds[0]], points[pIds[1]]);

                    console.log(d1 - d2);

                    valueAccessor()(viewModel, event);
                }, true);*/
            }
        };
    }
}

export = MyZoom;
